﻿using System;
using System.Collections;
using System.IO;
using UnityEngine;
using XMainClient.UI;
using XMainClient.UI.UICommon;
using XUpdater;
using XUtliPoolLib;

namespace XMainClient
{
	// Token: 0x02000B3A RID: 2874
	internal class XScreenShotMgr : XSingleton<XScreenShotMgr>
	{
		// Token: 0x17003018 RID: 12312
		// (get) Token: 0x0600A7FD RID: 43005 RVA: 0x001DDF74 File Offset: 0x001DC174
		public string FilePath
		{
			get
			{
				return this._file_path;
			}
		}

		// Token: 0x17003019 RID: 12313
		// (get) Token: 0x0600A7FE RID: 43006 RVA: 0x001DDF8C File Offset: 0x001DC18C
		public string FileName
		{
			get
			{
				return this._file_name;
			}
		}

		// Token: 0x0600A7FF RID: 43007 RVA: 0x001DDFA4 File Offset: 0x001DC1A4
		public override bool Init()
		{
			bool flag = this._ifly_mgr == null;
			if (flag)
			{
				this._ifly_mgr = (XUpdater.XUpdater.XGameRoot.GetComponent("XIFlyMgr") as IXIFlyMgr);
			}
			this._logo_local_path = Application.persistentDataPath + "/sharelogo";
			this.ClearOldPic();
			return true;
		}

		// Token: 0x0600A800 RID: 43008 RVA: 0x001DDFFA File Offset: 0x001DC1FA
		public override void Uninit()
		{
			base.Uninit();
		}

		// Token: 0x0600A801 RID: 43009 RVA: 0x001DE004 File Offset: 0x001DC204
		private void CheckScreenShotPic(object obj)
		{
			bool flag = this._ifly_mgr == null;
			if (!flag)
			{
				bool flag2 = File.Exists(this._file_path);
				if (flag2)
				{
					this._is_sharing = false;
					bool flag3 = XSingleton<XLoginDocument>.singleton.Channel == XAuthorizationChannel.XAuthorization_QQ;
					if (flag3)
					{
						this._ifly_mgr.ScreenShotQQShare(this._file_path, this._share_type);
					}
					else
					{
						this._ifly_mgr.ScreenShotWeChatShare(this._file_path, this._share_type);
					}
					DlgBase<ScreenShotShareView, ScreenShotShareBehaviour>.singleton.SetVisible(false, true);
				}
				else
				{
					bool flag4 = this._check_count >= this._max_check_count;
					if (flag4)
					{
						XSingleton<XDebug>.singleton.AddErrorLog("Share failed", null, null, null, null, null);
					}
					else
					{
						this._check_token = XSingleton<XTimerMgr>.singleton.SetTimer(0.1f, new XTimerMgr.ElapsedEventHandler(this.CheckScreenShotPic), null);
						this._check_count++;
					}
				}
			}
		}

		// Token: 0x0600A802 RID: 43010 RVA: 0x001DE0F0 File Offset: 0x001DC2F0
		public void CaptureScreenshot(object obj)
		{
			XSingleton<XScreenShotMgr>.singleton.SendStatisticToServer(ShareOpType.Photo, DragonShareType.None);
			this._file_name = string.Format("{0}{1}{2}_{3}{4}{5}.png", new object[]
			{
				DateTime.Now.Year.ToString().PadLeft(2, '0'),
				DateTime.Now.Month.ToString().PadLeft(2, '0'),
				DateTime.Now.Day.ToString().PadLeft(2, '0'),
				DateTime.Now.Hour.ToString().PadLeft(2, '0'),
				DateTime.Now.Minute.ToString().PadLeft(2, '0'),
				DateTime.Now.Second.ToString().PadLeft(2, '0')
			});
			this._file_path = Application.persistentDataPath + "/" + this._file_name;
			XSingleton<XDebug>.singleton.AddLog("Take a screen shot: ", this._file_path, null, null, null, null, XDebugColor.XDebug_None);
			RuntimePlatform platform = Application.platform;
			if (platform != RuntimePlatform.IPhonePlayer && platform != RuntimePlatform.Android)
			{
				ScreenCapture.CaptureScreenshot(this._file_path, 0);
			}
			else
			{
				ScreenCapture.CaptureScreenshot(this._file_name, 0);
			}
		}

		// Token: 0x0600A803 RID: 43011 RVA: 0x001DE248 File Offset: 0x001DC448
		public void ShareScreen(bool issession)
		{
			if (issession)
			{
				this._share_type = "Session";
			}
			else
			{
				bool flag = XSingleton<XLoginDocument>.singleton.Channel == XAuthorizationChannel.XAuthorization_QQ;
				if (flag)
				{
					this._share_type = "QZone";
				}
				else
				{
					this._share_type = "Timeline";
				}
			}
			bool is_sharing = this._is_sharing;
			if (!is_sharing)
			{
				this._check_count = 0;
				this.CheckScreenShotPic(null);
			}
		}

		// Token: 0x0600A804 RID: 43012 RVA: 0x001DE2B0 File Offset: 0x001DC4B0
		public string PartCaptureScreen(Rect rect, string accountType, string scene)
		{
			this._share_type = scene;
			XSingleton<XScreenShotMgr>.singleton.SendStatisticToServer(ShareOpType.Share, DragonShareType.Pandora);
			XSingleton<XChatIFlyMgr>.singleton.IFLYMGR.GetMonoBehavior().StartCoroutine(this.CaptureByCamera(rect, accountType));
			return this._file_path;
		}

		// Token: 0x0600A805 RID: 43013 RVA: 0x001DE2F9 File Offset: 0x001DC4F9
		private IEnumerator CaptureByCamera(Rect rect, string accountType)
		{
			yield return new WaitForEndOfFrame();
			Texture2D texture = new Texture2D((int)rect.width, (int)rect.height);
			texture.ReadPixels(rect, 0, 0);
			texture.Apply();
			try
			{
				byte[] bytes = texture.EncodeToPNG();
				this._file_name = string.Format("{0}{1}{2}_{3}{4}{5}.png", new object[]
				{
					DateTime.Now.Year.ToString().PadLeft(2, '0'),
					DateTime.Now.Month.ToString().PadLeft(2, '0'),
					DateTime.Now.Day.ToString().PadLeft(2, '0'),
					DateTime.Now.Hour.ToString().PadLeft(2, '0'),
					DateTime.Now.Minute.ToString().PadLeft(2, '0'),
					DateTime.Now.Second.ToString().PadLeft(2, '0')
				});
				this._file_path = Application.persistentDataPath + "/" + this._file_name;
				File.WriteAllBytes(this._file_path, bytes);
				bytes = null;
			}
			catch (Exception ex)
			{
				Exception e = ex;
				XSingleton<XDebug>.singleton.AddErrorLog("part capture failed " + e.Message, null, null, null, null, null);
			}
			bool flag = File.Exists(this._file_path);
			if (flag)
			{
				bool flag2 = accountType == "qq";
				if (flag2)
				{
					this._ifly_mgr.ScreenShotQQShare(this._file_path, this._share_type);
				}
				else
				{
					this._ifly_mgr.ScreenShotWeChatShare(this._file_path, this._share_type);
				}
			}
			yield break;
		}

		// Token: 0x0600A806 RID: 43014 RVA: 0x001DE318 File Offset: 0x001DC518
		private void ClearOldPic()
		{
			string persistentDataPath = Application.persistentDataPath;
			bool flag = Directory.Exists(persistentDataPath);
			if (flag)
			{
				DirectoryInfo directoryInfo = new DirectoryInfo(persistentDataPath);
				FileInfo[] files = directoryInfo.GetFiles();
				bool flag2 = files != null;
				if (flag2)
				{
					for (int i = 0; i < files.Length; i++)
					{
						string a = files[i].Name.Substring(files[i].Name.LastIndexOf(".") + 1);
						bool flag3 = a != "png";
						if (!flag3)
						{
							files[i].Delete();
						}
					}
				}
			}
		}

		// Token: 0x0600A807 RID: 43015 RVA: 0x001DE3AF File Offset: 0x001DC5AF
		public void SaveScreenshotPic(string filepath)
		{
			this._ifly_mgr.ScreenShotSave(filepath);
		}

		// Token: 0x0600A808 RID: 43016 RVA: 0x001DE3BF File Offset: 0x001DC5BF
		public void RefreshPhotoView(string fullpath)
		{
			this._ifly_mgr.RefreshAndroidPhotoView(fullpath);
		}

		// Token: 0x0600A809 RID: 43017 RVA: 0x001DE3D0 File Offset: 0x001DC5D0
		public void StartExternalScreenShotView(ScreenShotCallback callback)
		{
			bool share_doing = this._share_doing;
			if (!share_doing)
			{
				this._share_callback = callback;
				this.CaptureScreenshot(null);
				this._share_token = XSingleton<XTimerMgr>.singleton.SetTimer(0.2f, new XTimerMgr.ElapsedEventHandler(this.ExternalReadyShare), null);
			}
		}

		// Token: 0x0600A80A RID: 43018 RVA: 0x001DE41C File Offset: 0x001DC61C
		private void ExternalReadyShare(object obj)
		{
			bool flag = !File.Exists(XSingleton<XScreenShotMgr>.singleton.FilePath);
			if (flag)
			{
				this._share_count++;
				bool flag2 = this._share_count > 30;
				if (flag2)
				{
					XSingleton<UiUtility>.singleton.ShowSystemTip("Failed", "fece00");
					this._share_doing = false;
				}
				else
				{
					this._share_token = XSingleton<XTimerMgr>.singleton.SetTimer(0.1f, new XTimerMgr.ElapsedEventHandler(this.ExternalReadyShare), null);
				}
			}
			else
			{
				XSingleton<XDebug>.singleton.AddLog("The screen file path: ", XSingleton<XScreenShotMgr>.singleton.FilePath, null, null, null, null, XDebugColor.XDebug_None);
				XSingleton<XDebug>.singleton.AddLog("File exist: ", File.Exists(XSingleton<XScreenShotMgr>.singleton.FilePath).ToString(), null, null, null, null, XDebugColor.XDebug_None);
				RuntimePlatform platform = Application.platform;
				if (platform == RuntimePlatform.IPhonePlayer || platform == RuntimePlatform.Android)
				{
					byte[] array = File.ReadAllBytes(XSingleton<XScreenShotMgr>.singleton.FilePath);
					Texture2D texture2D = new Texture2D(2, 2);
					bool flag3 = texture2D.LoadImage(array);
					XSingleton<XDebug>.singleton.AddLog("Use file load", null, null, null, null, null, XDebugColor.XDebug_None);
					bool flag4 = flag3;
					if (flag4)
					{
						int width = texture2D.width;
						int height = texture2D.height;
						XSingleton<XDebug>.singleton.AddLog("The width: ", width.ToString(), ", height: ", height.ToString(), null, null, XDebugColor.XDebug_None);
						bool flag5 = width <= 600 && this._share_callback != null;
						if (flag5)
						{
							this._share_callback(false);
						}
						else
						{
							this.DoShareAction();
						}
                        UnityEngine.Object.DestroyImmediate(texture2D, true);
					}
					else
					{
						bool flag6 = !flag3 && this._share_callback != null;
						if (flag6)
						{
							this._share_callback(false);
						}
					}
					this._share_doing = false;
				}
			}
		}

		// Token: 0x0600A80B RID: 43019 RVA: 0x001DE5F3 File Offset: 0x001DC7F3
		private void DoShareAction()
		{
			this.ShareScreen(false);
		}

		// Token: 0x0600A80C RID: 43020 RVA: 0x001DE5FE File Offset: 0x001DC7FE
		private IEnumerator StartDownloadLogo(string path)
		{
			XSingleton<XDebug>.singleton.AddLog("StartDownloadLogo", null, null, null, null, null, XDebugColor.XDebug_None);
			this._downloader = new WWW(path);
			yield return this._downloader;
			while (!this._downloader.isDone)
			{
				yield return this._downloader;
			}
			XSingleton<XDebug>.singleton.AddLog("Will do share, local icon: ", this._logo_local_path, null, null, null, null, XDebugColor.XDebug_None);
			XSingleton<XDebug>.singleton.AddLog("IsDone: ", this._downloader.isDone.ToString(), ", error: ", this._downloader.error, null, null, XDebugColor.XDebug_None);
			bool flag = this._downloader.isDone && string.IsNullOrEmpty(this._downloader.error);
			if (flag)
			{
				byte[] bs = this._downloader.bytes;
				bool flag2 = bs != null;
				if (flag2)
				{
					try
					{
						File.WriteAllBytes(this._logo_local_path, bs);
					}
					catch (Exception ex)
					{
						Exception e = ex;
						XSingleton<XDebug>.singleton.AddErrorLog("write file local failed!", e.Message, null, null, null, null);
					}
					XSingleton<XDebug>.singleton.AddLog("Is icon exsit: ", File.Exists(this._logo_local_path).ToString(), null, null, null, null, XDebugColor.XDebug_None);
					bool flag3 = File.Exists(this._logo_local_path);
					if (flag3)
					{
						this.DownLoadCallback();
					}
				}
				bs = null;
			}
			this._downloader.Dispose();
			this._downloader = null;
			yield break;
		}

		// Token: 0x0600A80D RID: 43021 RVA: 0x001DE614 File Offset: 0x001DC814
		private void DownLoadCallback()
		{
			bool flag = this._ifly_mgr == null;
			if (!flag)
			{
				ShareTable.RowData shareInfoById = XScreenShotShareDocument.GetShareInfoById(this._share_index);
				string[] array = shareInfoById.Link.Split(new char[]
				{
					'|'
				});
				XSingleton<XDebug>.singleton.AddLog("Links: ", shareInfoById.Link, null, null, null, null, XDebugColor.XDebug_None);
				bool flag2 = XSingleton<XLoginDocument>.singleton.Channel == XAuthorizationChannel.XAuthorization_QQ;
				if (flag2)
				{
					string text = this._url;
					bool flag3 = string.IsNullOrEmpty(text);
					if (flag3)
					{
						text = array[0];
						text = string.Format(text, this._params);
					}
					this._ifly_mgr.ShareQZoneLink(shareInfoById.Title, shareInfoById.Desc, text, this._logo_local_path, this._share_session);
				}
				else
				{
					bool flag4 = XSingleton<XLoginDocument>.singleton.Channel == XAuthorizationChannel.XAuthorization_WeChat;
					if (flag4)
					{
						string text2 = this._url;
						bool flag5 = string.IsNullOrEmpty(text2);
						if (flag5)
						{
							text2 = ((array.Length > 1) ? array[1] : array[0]);
							text2 = string.Format(text2, this._params);
						}
						bool flag6 = this._tag == ShareTagType.Invite_Tag;
						if (flag6)
						{
							this._ifly_mgr.ShareWechatLink(shareInfoById.Title, this._logo_local_path, text2, this._share_session);
						}
						else
						{
							bool flag7 = this._tag == ShareTagType.GiftBag_Tag;
							if (flag7)
							{
								this._ifly_mgr.ShareWechatLinkWithMediaTag(shareInfoById.Title, this._logo_local_path, text2, this._share_session, "MSG_GIFT_PACKS_FD");
							}
						}
					}
				}
			}
		}

		// Token: 0x0600A80E RID: 43022 RVA: 0x001DE790 File Offset: 0x001DC990
		public void DoShareWithLink(int shareindex, bool issession = false, ShareTagType tag = ShareTagType.Invite_Tag, string url = null, params object[] args)
		{
			this.SendStatisticToServer(ShareOpType.Share, DragonShareType.ActivityShare);
			ShareTable.RowData shareInfoById = XScreenShotShareDocument.GetShareInfoById(shareindex);
			XSingleton<XDebug>.singleton.AddLog("Will doshare with link", null, null, null, null, null, XDebugColor.XDebug_None);
			bool flag = shareInfoById != null;
			if (flag)
			{
				this._share_index = shareindex;
				this._share_session = issession;
				this._tag = tag;
				this._params = args;
				this._url = url;
				bool flag2 = File.Exists(this._logo_local_path);
				if (flag2)
				{
					File.Delete(this._logo_local_path);
				}
				XSingleton<XDebug>.singleton.AddLog("Icon: ", shareInfoById.Icon, null, null, null, null, XDebugColor.XDebug_None);
				XSingleton<XChatIFlyMgr>.singleton.IFLYMGR.GetMonoBehavior().StartCoroutine(this.StartDownloadLogo(shareInfoById.Icon));
			}
		}

		// Token: 0x0600A80F RID: 43023 RVA: 0x001DE84C File Offset: 0x001DCA4C
		public void SendStatisticToServer(ShareOpType op, DragonShareType type)
		{
			PtcC2G_ShareReport ptcC2G_ShareReport = new PtcC2G_ShareReport();
			ptcC2G_ShareReport.Data.type = (int)type;
			ptcC2G_ShareReport.Data.op = (int)type;
			XSingleton<XClientNetwork>.singleton.Send(ptcC2G_ShareReport);
		}

		// Token: 0x04003E2E RID: 15918
		private string _file_name;

		// Token: 0x04003E2F RID: 15919
		private string _file_path;

		// Token: 0x04003E30 RID: 15920
		private string _share_type;

		// Token: 0x04003E31 RID: 15921
		private int _check_count = 0;

		// Token: 0x04003E32 RID: 15922
		private int _max_check_count = 50;

		// Token: 0x04003E33 RID: 15923
		private bool _is_sharing = false;

		// Token: 0x04003E34 RID: 15924
		private IXIFlyMgr _ifly_mgr = null;

		// Token: 0x04003E35 RID: 15925
		private uint _check_token = 0U;

		// Token: 0x04003E36 RID: 15926
		private uint _share_token = 0U;

		// Token: 0x04003E37 RID: 15927
		private int _share_count = 0;

		// Token: 0x04003E38 RID: 15928
		private bool _share_doing = false;

		// Token: 0x04003E39 RID: 15929
		private string _logo_local_path;

		// Token: 0x04003E3A RID: 15930
		private int _share_index = 1;

		// Token: 0x04003E3B RID: 15931
		private WWW _downloader = null;

		// Token: 0x04003E3C RID: 15932
		private bool _share_session = false;

		// Token: 0x04003E3D RID: 15933
		private ShareTagType _tag = ShareTagType.Invite_Tag;

		// Token: 0x04003E3E RID: 15934
		private string _url = null;

		// Token: 0x04003E3F RID: 15935
		private object[] _params;

		// Token: 0x04003E40 RID: 15936
		private ScreenShotCallback _share_callback = null;
	}
}
